package ccpv;

import java.util.HashMap;

public class GroupSet implements GroupSetInterface {

	private HashMap<String, GroupInterface> groups = new HashMap<String, GroupInterface>();
	private HashMap<GroupPairInterface, Float> pairGroupnesses =
		new HashMap<GroupPairInterface, Float>();
	private int totalPopulation = -1;
	
	/* (non-Javadoc)
	 * @see ccpv.GroupSetInterface#addGroup(ccpv.GroupInterface)
	 */
	public void addGroup(GroupInterface  group){
		groups.put(group.getGroupName(), group);
	}
	
	public void addPairGroupness(GroupPairInterface  groupPair, float f){
		pairGroupnesses.put(groupPair, f);
	}	
	
	/* (non-Javadoc)
	 * @see ccpv.GroupSetInterface#getTotalPopulation()
	 */
	public int getTotalPopulation(){
		if(totalPopulation == -1){
			int populationCounter = 0;
			for(String s : groups.keySet()){
				populationCounter += groups.get(s).getSize();
			}
			totalPopulation = populationCounter;
		}
		return totalPopulation;
	}
	
	/* (non-Javadoc)
	 * @see ccpv.GroupSetInterface#getAllianceSize(java.util.HashMap)
	 */
	public int getAllianceSize(HashMap<String, GroupInterface> allianceGroups){
		int populationCounter = 0;
		for(String s : allianceGroups.keySet()){
			populationCounter += groups.get(s).getSize();
		}	
		return populationCounter;
	}
	
	/* (non-Javadoc)
	 * @see ccpv.GroupSetInterface#getAllianceSizeByPower(java.util.HashMap)
	 */
	public float getAllianceSizeByPower(HashMap<String, GroupInterface> allianceGroups){
		int power = 0;
		for(String s : allianceGroups.keySet()){
			power += groups.get(s).getSize()*groups.get(s).getPowerPerIndividual();
		}	
		return power;
	}	
	
	/* (non-Javadoc)
	 * @see ccpv.GroupSetInterface#getDesirabilityToJoin(ccpv.GroupInterface, ccpv.GroupInterface)
	 */
	public float getDesirabilityToJoin(GroupInterface a, GroupInterface b){
		return 1.0f/(float)(a.getSize() + b.getSize()) + a.getGroupness(); 
	}
	
	
}
